/*
 * Copyright (c) 2010, Michael Hengler (developer (at) hengler (dot) de),
 * Nikolaus Moll (developer (at) miblounge (dot) net).
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the [QTPRJ] nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL [QTPRJ] BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package joodin.impl.widgets;

import joodin.impl.application.util.ApplicationWrapper;
import joodin.impl.image.VaadinImageRegistry;
import joodin.impl.util.DimensionConvert;
import my.stuff.vaadin.miglayout.MigLayout;
import my.stuff.vaadin.miglayout.util.ComponentSizeUtil;
import my.stuff.vaadin.miglayout.util.Size;
import my.stuff.vaadin.miglayout.util.SizeConverter;

import org.jowidgets.common.image.IImageConstant;
import org.jowidgets.common.types.Dimension;
import org.jowidgets.common.types.Position;
import org.jowidgets.common.types.Rectangle;
import org.jowidgets.common.widgets.IDisplayCommon;
import org.jowidgets.common.widgets.controler.IWindowListener;
import org.jowidgets.common.widgets.descriptor.IWidgetDescriptor;
import org.jowidgets.common.widgets.factory.IGenericWidgetFactory;
import org.jowidgets.spi.impl.controler.WindowObservable;
import org.jowidgets.spi.widgets.IWindowSpi;

import com.vaadin.event.FieldEvents.BlurEvent;
import com.vaadin.event.FieldEvents.BlurListener;
import com.vaadin.event.FieldEvents.FocusEvent;
import com.vaadin.event.FieldEvents.FocusListener;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
import com.vaadin.terminal.gwt.server.WebBrowser;
import com.vaadin.ui.Window;

public class VaadinWindow extends VaadinContainer implements IWindowSpi {

	private final WindowObservable windowObservableDelegate;

	public VaadinWindow(final IGenericWidgetFactory factory, final Window window) {
		super(factory, window);

		this.windowObservableDelegate = new WindowObservable();
		
		createListener();
	}
	
	@Override
	public Window getUiReference() {
		return (Window) super.getUiReference();
	}
	
	// TODO: check if this is valid
	@Override
	public void pack() { 
		if(!(getUiReference().getContent() instanceof MigLayout) || doLayout == false){
			return;
		}

		Size size = ComponentSizeUtil.getPreferredSize(getUiReference());
		
		getUiReference().setWidth(size.getWidth(), Sizeable.UNITS_PIXELS);
		getUiReference().setHeight(size.getHeight(), Sizeable.UNITS_PIXELS);
		
		((MigLayout)getUiReference().getContent()).layout(true);
		
	}

	@Override
	public void setVisible(final boolean visible) {
		getUiReference().setVisible(visible);
//		getUiReference().bringToFront();
	}

	@Override
	public boolean isVisible() {
		return getUiReference().isVisible();
	}

	@Override
	public final void setPosition(final Position position) {
//		((Window)getUiReference()).setPositionX(position.getX());
//		((Window)getUiReference()).setPositionY(position.getY());
		getUiReference().center();
	}

	@Override
	public Position getPosition() {
		return new Position(((Window)getUiReference()).getPositionX(), ((Window)getUiReference()).getPositionY());
	}

	@Override
	public void setSize(final Dimension size) {
		((Window)getUiReference()).setWidth(size.getWidth(), Sizeable.UNITS_PIXELS);
		((Window)getUiReference()).setHeight(size.getHeight(), Sizeable.UNITS_PIXELS);
	}

	@Override
	public Dimension getSize() {
		return new Dimension(SizeConverter.getPixelWidth(getUiReference()), SizeConverter.getPixelHeight(getUiReference()));
	}

	@Override
	public <WIDGET_TYPE extends IDisplayCommon, DESCRIPTOR_TYPE extends IWidgetDescriptor<? extends WIDGET_TYPE>> WIDGET_TYPE createChildWindow(
		final DESCRIPTOR_TYPE descriptor) {
		return getGenericWidgetFactory().create(getUiReference(), descriptor);
	}

	protected void setIcon(final IImageConstant icon, final VaadinImageRegistry imageRegistry) {
		// TODO: implement
//		getUiReference().setIcon(imageRegistry.getImage(icon).getIcon());
	}

	@Override
	public Rectangle getParentBounds() {
		Dimension parentSize = null;
		Position parentPosition = new Position(0, 0);
		if(getUiReference().getParent() == ApplicationWrapper.getInstance().getMainWindow()){
			WebApplicationContext context = ((WebApplicationContext)ApplicationWrapper.getInstance().getContext());
			WebBrowser browser = context.getBrowser();
			parentSize = new Dimension(browser.getScreenWidth(), browser.getScreenHeight());
		}
		else if (getUiReference().getParent() != null) {
			parentPosition = new Position(((Window)getUiReference()).getParent().getPositionX(), ((Window)getUiReference()).getParent().getPositionY());
			parentSize = new Dimension(SizeConverter.getPixelWidth(getUiReference().getParent()), SizeConverter.getPixelHeight(getUiReference().getParent()));
		} else {
			parentSize = DimensionConvert.convert(SizeConverter.getPixelWidth(getUiReference()), SizeConverter.getPixelHeight(getUiReference()));
		}
		return new Rectangle(parentPosition, parentSize);
	}

	@Override
	public void addWindowListener(final IWindowListener listener) {
		windowObservableDelegate.addWindowListener(listener);
	}

	@Override
	public void removeWindowListener(final IWindowListener listener) {
		windowObservableDelegate.removeWindowListener(listener);
	}

	@Override
	public void dispose() {
//		getUiReference().setVisible(false);
	}
	@SuppressWarnings("serial")
	private void createListener(){
		// Register events
		getUiReference().addListener(new FocusListener() {
			@Override
			public void focus(FocusEvent event) {
				windowObservableDelegate.fireWindowActivated();
			}
		});
		
		getUiReference().addListener(new BlurListener() {
			@Override
			public void blur(BlurEvent event) {
				windowObservableDelegate.fireWindowDeactivated();				
			}
		});

	}
}
